package com.inspectortime.webapp.inspector.billing;

import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.inspectortime.domain.billing.Invoice;
import com.inspectortime.domain.billing.Payment;
import com.inspectortime.repository.Repository;

public class ViewInvoiceDetailController extends AbstractController {
	
	private static Log log = LogFactory.getLog(ViewInvoiceDetailController.class);
	
	private Repository repository;
	private String returnView;
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Serializable invoiceId = request.getParameter("id");

		Invoice invoice = repository.loadInvoice(invoiceId);
		Payment payment = repository.findCompletedPaymentBySource((String) invoiceId);
		
		ModelAndView mv = new ModelAndView(getReturnView());
		mv.addObject("invoice", invoice);
		if (payment != null) {
			mv.addObject("payment", payment);	
		}		
		return mv;
	}
	
	public void setRepository(Repository repository) {
		this.repository = repository;
	}

	public String getReturnView() {
		return returnView;
	}

	public void setReturnView(String returnView) {
		this.returnView = returnView;
	}

	
	
	

}
